import os
import re

import requests

image = 'image'
if not os.path.exists(image):
    os.mkdir(image)
    
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36'
}
response = requests.get('https://qq.yh31.com/zjbq/', headers=headers)
response.encoding = 'utf-8'
# print(response.request.headers)
# print(response.status_code)
t = '<img src="(.*?)" alt="(.*?)" width="160" height="120">'
result = re.findall(t, response.text)


def getimg(temp_img_num, result, image):
    temp = 0

    for img in result:
        if temp < temp_img_num:
            print(img)
            res = requests.get(img[0])
            # print(res.status_code)
            s = img[0].split('.')[-1]
            with open(image + '/' + img[1] + '.' + s, mode='wb') as file:
                temp += 1
                file.write(res.content)
        else:
            print('下载完成！！！')
            break


temp_img_num = int(input('输入获取图片数量:'))
getimg(temp_img_num, result, image)
